<?php
namespace Home\Controller;
class LawyerCenterController extends CommonController{
	public function _initialize(){
		parent::_initialize();
		$this->user_id();
	}

	public function index(){
		// 判断是否完善资料
		if(!$this->user['info_is_full']){
			redirect(U('LawyerCenter/perfect_data'));
		}
		// 已完善资料的跳转订单中心
		redirect(U('LawyerOrder/index', array('type' => 'all')));
	}
	public function perfect_data(){
		$this->add_step('资料完善');
		if(IS_POST){
			$realname          = $this->_post('realname', '姓名不能为空！');
			$nickname          = $this->_post('nickname');
			$sex               = $this->_post('sex');
			$city_id           = $this->_post('city_id', '请选择城市！');
			$area_id           = $this->_post('area_id');
			$identification_id = $this->_post('identification_id', '请上传证件照！');
			$practice_no       = $this->_post('practice_no', '执业证号不能为空！');
			$work_unit         = $this->_post('work_unit', '工作单位不能为空！');
			$skilled_id        = $this->_post('skilled_id', '请选择擅长领域！');

			$identification_id = implode(',', $identification_id);
			if(substr($identification_id, 0, 2) == '0,'){
				$identification_id = substr($identification_id, 2);
			}
			if(D('User')->edit_info($this->user_id, $realname, $nickname, $sex, $city_id, $area_id)){
				D('User')->edit_lawyer_info($this->user_id, $identification_id, $practice_no, $work_unit, $skilled_id);
				$this->success('修改成功！', U('index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//个人信息
	public function basic_info(){
		if($this->user['lawyer_info']['audit_status'] == 0){
			$this->error('对不起，审核中，无法操作！');
		}
		$this->add_step('个人信息');
		if(IS_POST){
			$realname          = $this->_post('realname');
			$nickname          = $this->_post('nickname');
			$sex               = $this->_post('sex');
			$city_id           = $this->_post('city_id');
			$area_id           = $this->_post('area_id');
			// 律师信息
			$practice_no       = $this->_post('practice_no', '执业证号不能为空！');
			$work_unit         = $this->_post('work_unit', '工作单位不能为空！');
			$skilled_id        = $this->_post('skilled_id', '请选择擅长领域！');
			if(D('User')->edit_info($this->user_id, $realname, $nickname, $sex, $city_id, $area_id)){
				D('User')->edit_lawyer_info($this->user_id, '', $practice_no, $work_unit, $skilled_id);
				$this->success('修改成功！', URL('basic_info'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->assign('lawyer_info', $this->user['lawyer_info']);
		$this->assign('info', $this->user);
		$this->display();
	}
	//更换手机号
	public function change_mobile(){
		$this->add_step($this->user_mobile ? '更换手机号' : '绑定手机号');
		if(!$this->set_password){
			$this->error('请先设置密码！');
		}
		if(IS_POST){
			$mobile   = $this->set_password ? $this->_post('mobile', '手机号不能为空！') : '';
			$smscode  = $this->_post('smscode', '短信验证码不能为空！');
			$password = $this->set_password ? $this->_post('password', '密码不能为空！') : '';
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_mobile($this->user_id, $mobile)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！', URL('index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}
	//更换E-mail
	public function change_email(){
		$this->add_step('更换E-mail');
		if(!$this->set_password){
			$this->error('请先设置密码！');
		}
		if(IS_POST){
			$email    = $this->_post('email', 'E-mail不能为空！');
			$password = $this->_post('password', '密码不能为空！');
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_email($this->user_id, $email)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！',  URL('index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}
	//修改头像
	public function upload_head(){
		if($this->user['lawyer_info']['audit_status'] == 0){
			$this->error('对不起，审核中，无法操作！');
		}
		$this->add_step('修改头像');
		if(IS_POST){
			$file_id = $this->_post('file_id', '请上传照片');
			$x1      = $this->_post('x1', '', 0);
			$y1      = $this->_post('y1', '', 0);
			$x2      = $this->_post('x2', '请上传照片');
			$y2      = $this->_post('y2', '请上传照片');
			$width   = $this->_post('width', '请上传照片');
			$height  = $this->_post('height', '请上传照片');
			if(D('User')->user_head_crop($this->user_id, $file_id, $x1, $y1, $x2, $y2, $width, $height)){
				$this->success('保存成功！', URL('upload_head'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//修改密码
	public function change_password(){
		$this->add_step('修改密码');
		if(IS_POST){
			$old_password = $this->set_password ? $this->_post('old_password', '旧密码不能为空！') : '';
			$new_password = $this->_post('password', '新密码不能为空！');

			$old_password = md5($old_password);
			$new_password = md5($new_password);
			if(D('User')->change_password($this->user_id, $old_password, $new_password)){
				$this->success('修改成功！', U('index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
}